<template lang="">
    <div>
        <van-field label='标题' v-model='form.title'/>
        <van-field label='描述' v-model='form.descrip'/>
        <van-field label='图片' v-model='form.pic'/>
        上传图片<van-uploader :after-upload="upload"/>
        选择讲师
        <select v-model='form.teacher_id'>
            <option :value="i.id" v-for='i in tlist'>{{i.name}}</option>
        </select>
        选择分类
        <reply :data='cates'/>
        动态参数
        <div v-if='params'>
                <van-field v-for="(v,k) in params" :label='v' v-model="values[v]"/>
        </div>
        <van-field name="radio" label="状态">
                <template #input>
                  <van-radio-group v-model="form.status" direction="horizontal">
                    <van-radio name="1">免费</van-radio>
                    <van-radio name="2">收费</van-radio>
                  </van-radio-group>
                </template>
              </van-field>

        <van-button @click='add'>添加</van-button>
    </div>
</template>
<script>
import bus from './bus.js'
import reply from './reply'
export default {
    components:{
        'reply':reply
    },
    data(){
        return{
            tlist:[{'id':1,'name':'王老师'},{'id':2,'name':'李老师'},{'id':3,'name':'孙老师'}],
            cates:{},
            params:{},
            values:{},
            form:{pid:0,params:'',status:'2',pic:''}
        }
    },
    methods: {
        //获取分类列表
        getlist(){
            this.axios.get('cateM').then(res=>{
                this.cates={'id':0,'name':'sdfdsf','son':res.data.list}
            })
        },
        add(){
            if(this.values){
                this.form.params = JSON.stringify(this.values)
            }
            this.axios.post('courseM',this.form).then(res=>{

            })
        },
        //获取token
        gettoken(){
            this.axios.get('getToken').then(res=>{
                this.token = re.data.token
            })
        },
        //文件上传
        upload(file){
            let formdata = new FormData()
            formdata.append('file',file.file)
            formdata.append('token',this.token)
            this.axios.post('http://up-z1.qiniu.com/',formdata).then(res=>{
                this.form.pic = 'http://r33w954v7.hb-bkt.clouddn.com/'+res.data.key
            }).catch(error=>{
                console.log(error)
            })
        }
    },
    mounted() {
        this.gettoken()
        this.getlist()
        bus.$on('pid',val=>{
            this.form.pid = val
            //通过pid查询分类params
            this.axios.get('getCateByid?id='+val).then(res=>{
                if(res.data.params){
                    this.params = JSON.parse(res.data.params)
                }else{
                    this.params = {}
                }
                
            })
        })
    },
}
</script>
<style lang="">
    
</style>